/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ftpcombiner.gui;

import ftpcombiner.ApplicationController;
import ftpcombiner.gui.applicationframe.FrameSplitter;
import ftpcombiner.gui.applicationframe.MenuBar;
import ftpcombiner.gui.applicationframe.StatusBar;
import ftpcombiner.gui.applicationframe.ToolBar;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author Ben
 */
public class ApplicationFrame extends JFrame implements GUILoader {

    private MenuBar menuBar;
    private FrameSplitter frameSplitter;
    private ToolBar toolBar;
    private StatusBar statusBar;

    @Override
    public void initializeComponents() {
        menuBar = new MenuBar();
        frameSplitter = new FrameSplitter();
        toolBar = new ToolBar();
        statusBar = new StatusBar();
    }

    @Override
    public void addComponents() {
        setJMenuBar(menuBar);
        add(frameSplitter, BorderLayout.CENTER);
        add(toolBar, BorderLayout.NORTH);
        add(statusBar, BorderLayout.SOUTH);
    }

    @Override
    public void addComponentListeners() {
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                //System.out.println("closing");
                ApplicationController.getInstance().runOnExit();
            }
        });
    }

    @Override
    public void otherInit() {
        //revalidate();
        setTitle("FTP Combiner " + ApplicationController.Version.getPrettyVersionNumber());
        setLocationByPlatform(true);
        setIconImage(GUIResources.getInstance().getAppIconImage());
        pack();
        setSize(new Dimension(700, 500));
        setVisible(true);
    }

    private void init() {
        System.out.println("Loading application frame...");
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
            System.err.println("Error setting native LAF: " + e);
        }
        initializeComponents();
        setLayouts();
        addComponents();
        addComponentListeners();
        otherInit();
        System.out.println("Finished loading application frame");
        ApplicationController.getSplashScreen().incrementProgress();
    }

    public ApplicationFrame() {
        init();
    }

    @Override
    public void setLayouts() {
        setLayout(new BorderLayout());
    }

    public void updateFileList() {
        frameSplitter.updateFileList();
    }

    public FrameSplitter getFrameSplitter() {
        return frameSplitter;
    }

    public StatusBar getStatusBar() {
        return statusBar;
    }
}
